package com.android.htdvideoview.extra;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.widget.FrameLayout;

import com.sansecy.android.simplevideo.SimpleVideoView;

public class SimplePlayerView  {
    private static final String TAG = "SimplePlayerView-App";
    private final SimpleVideoView videoView;
    private String videoUrl;

    public SimplePlayerView(Context context, FrameLayout playerWrapper, iICntvPlayInterface playListener) {
        videoView = new SimpleVideoView(context);
        int width = playerWrapper.getWidth();
        int height = playerWrapper.getHeight();
        Log.d(this.getClass().getName() + "-App", "SimplePlayerView:playerWrapper width = " + width + " height = " + height);
        playerWrapper.post(new Runnable() {
            @Override
            public void run() {
                int width = playerWrapper.getWidth();
                int height = playerWrapper.getHeight();
                Log.d(this.getClass().getName() + "-App", "SimplePlayerView:playerWrapper width = " + width + " height = " + height);
            }
        });
        playerWrapper.addView(videoView, -1, -1);
        videoView.post(new Runnable() {
            @Override
            public void run() {
                int width = videoView.getWidth();
                int height = videoView.getHeight();
                Log.d(this.getClass().getName() + "-App", "SimplePlayerView:videoView  width = " + width + " height = " + height);
            }
        });
        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                Log.d(TAG, "onInfo() called with: mp = [" + mp + "], what = [" + what + "], extra = [" + extra + "]");
                if (what == 701) {
                    playListener.onBufferStart(iICntvPlayInterface.ON_BUFFER_START_TYPE_VIDEO);
                } else if (what == 702) {
                    playListener.onBufferEnd(iICntvPlayInterface.ON_BUFFER_START_TYPE_VIDEO);
                }
                return false;
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, "onCompletion() called with: mp = [" + mp + "]");
                playListener.onCompletion();
            }
        });
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d(TAG, "onError() called with: mp = [" + mp + "], what = [" + what + "], extra = [" + extra + "]");
                playListener.onError(0, 0, "");
                return false;
            }
        });
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
               Log.d(TAG, "onPrepared() called with: mp = [" + mp + "]");
                playListener.onPrepared();
            }
        });
    }

    public int getCurrentPosition() {
        Log.d(TAG, "getCurrentPosition() called");
        return videoView.getCurrentPosition();
    }

    public boolean isADPlaying() {
        Log.d(TAG, "isADPlaying() called");
        return false;
    }

    public boolean isPlaying() {
        Log.d(TAG, "isPlaying() called");
        return videoView.isPlaying();
    }

    public void setDataSource(String videoUrl) {
        Log.d(TAG, "setDataSource() called with: videoUrl = [" + videoUrl + "]");
        this.videoUrl = videoUrl;
    }

    public void seekTo(int position) {
        Log.d(TAG, "seekTo() called with: position = [" + position + "]");
        videoView.seekTo(position);
    }

    public void release() {
        Log.d(TAG, "release() called");
        videoView.stopPlayback();
    }

    public void pauseVideo() {
        Log.d(TAG, "pauseVideo() called");
        videoView.pause();
    }

    public void startVideo() {
        Log.d(TAG, "startVideo() called " + videoUrl + " videoView.getUrl(): " + videoUrl);
        videoView.loadUrl(videoUrl);
        videoView.start();
    }

    public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    public float getDuration() {
        Log.d(TAG, "getDuration() called");
        return videoView.getDuration();
    }
}
